package com.app.piemoment;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

import com.app.piemoment.config.Config;
import com.app.piemoment.entity.OppositeInfo;
import com.app.piemoment.entity.Photo;
import com.app.piemoment.image.ImageLoaderManager;

public class AlbumActivity extends BaseActivity {

	private Gallery gallery;
	private ArrayList<Photo> photos;

	public AlbumActivity() {
		mainViewId = R.layout.album_activity;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_left:
			super.onBackPressed();
			break;

		default:
			break;
		}
	}

	@Override
	protected void initMainView() {
		initTitle(R.drawable.btn_back_bg, "相册", 0,0);
		
		gallery = (Gallery) findViewById(R.id.gallery1);

		OppositeInfo oppInfo = getIntent().getParcelableExtra("photo");
		if (oppInfo != null) {
			photos = oppInfo.getPhotos();
		}
		
		ImageAdapter adapter = new ImageAdapter(this);
		gallery.setAdapter(adapter);

	}

	@Override
	protected void registerMainViewEvent() {

	}

	@Override
	protected void processMainView() {

	}

	public class ImageAdapter extends BaseAdapter {
		int mGalleryItemBackground;
		private Context mContext;
		private ImageLoaderManager imageloader;
		public ImageAdapter(Context context){
			this.mContext = context;
			imageloader = new ImageLoaderManager(AlbumActivity.this,new Handler());
			
		}

		// 返回图像总数
		public int getCount() {
			return photos!=null?photos.size():0;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		// 返回具体位置的ImageView对象
		public View getView(int position, View convertView, ViewGroup parent) {
			Photo photo = photos.get(position);
			ImageView imageView = null;
			if(convertView==null) {
				imageView = new ImageView(mContext);
				imageView.setScaleType(ImageView.ScaleType.FIT_XY);
				convertView = imageView;
			}else {
				imageView = (ImageView) convertView;
			}
			// 设置当前图像的图像（position为当前图像列表的位置）
			imageView.setBackgroundDrawable(new BitmapDrawable(imageloader.getImage(Config.IMAGE_URL+photo.getFilepath(), R.drawable.pm_default, 0)));
			return imageView;
		}
	}
}
